package db

import (
	"errors"
	"log"
	"server_cluster/battle/sharemem"
	"strconv"
	"strings"
)

type Config_buff struct {
	Id         int32  `gorm:"primary_key;column:id;autoIncrement:false" csv:"Id"`
	Type       int8   `gorm:"column:type" csv:"Type"`
	Sub_type   int8   `gorm:"column:sub_type" csv:"SubType"`
	Group      string `gorm:"column:group" csv:"Groupid"`
	Tag        int8   `gorm:"column:tag" csv:"BuffTag,omitempty"`
	Ctype      int8   `gorm:"column:ctype" csv:"Condition_Type,omitempty"`
	Condition  string `gorm:"column:condition" csv:"Condition,omitempty"`
	Loop_time  int32  `gorm:"column:loop_time" csv:"LoopTime,omitempty"`
	End_time   int32  `gorm:"column:end_time" csv:"Time"`
	Level_max  int8   `gorm:"column:level_max" csv:"BuffMaxLevel,omitempty"`
	Level_func int8   `gorm:"column:level_func" csv:"BuffLevelFunc,omitempty"`
	Param1     string `gorm:"column:param1" csv:"Param1,omitempty"`
}

func (tmp *Config_buff) TableName() string {
	return "config_buff"
}

func (tmp *Config_buff) getAll() ([]*Config_buff, error) {
	var data []*Config_buff
	result := Conn.Find(&data)

	return data, result.Error
}

func (tmp *Config_buff) LoadData() error {
	data, err := tmp.getAll()
	if err != nil {
		return err
	}
	var myshm = &(sharemem.Myshm.Cfg.Bf)
	for key, value := range data {
		if key >= sharemem.BUFF_MAX {
			return errors.New("config_buff format error 8")
		}
		myshm[key].Id = value.Id
		myshm[key].Type = value.Type
		myshm[key].Sub_type = value.Sub_type
		//
		if len(value.Group) > 0 {
			b_list := strings.Split(value.Group, ";")
			if len(b_list) > sharemem.BATTLE_PARAM_MAX2 {
				return errors.New("config_buff format error 1")
			}
			for k, v := range b_list {
				arg1, err1 := strconv.Atoi(v)
				if err1 != nil {
					return errors.New("config_buff format error 11")
				}
				if k == 0 {
					myshm[key].Group = int32(arg1)
				} else if k == 1 {
					myshm[key].Grade = int8(arg1)
				} else {
					break
				}
			}
		}
		myshm[key].Tag = value.Tag
		if value.Ctype == 1 {
			myshm[key].Ctype = false
		}
		if len(value.Condition) > 0 {
			a_list := strings.Split(value.Condition, ";")
			if len(a_list) > sharemem.BATTLE_PARAM_MAX1 {
				return errors.New("config_buff format error 0")
			}
			for k, v := range a_list {
				b_list := strings.Split(v, "|")
				if len(b_list) > sharemem.BATTLE_PARAM_MAX2 {
					return errors.New("config_buff format error 00")
				}
				for k1, v1 := range b_list {
					arg, err := strconv.Atoi(v1)
					if err != nil {
						return errors.New("config_buff format error 000")
					}
					myshm[key].Condition[k][k1] = int32(arg)
				}
			}
		}
		myshm[key].Loop_time = value.Loop_time
		if value.Sub_type == 2 || value.Sub_type == 3 {
			myshm[key].Loop_time = value.End_time
		}
		myshm[key].End_time = value.End_time
		if value.Level_max <= 0 {
			myshm[key].Level_max = 1
		} else {
			myshm[key].Level_max = value.Level_max
		}
		myshm[key].Level_func = value.Level_func
		if len(value.Param1) > 0 {
			b_list := strings.Split(value.Param1, ";")
			if len(b_list) > sharemem.BATTLE_PARAM_MAX2 {
				return errors.New("config_buff format error 1")
			}
			for k, v := range b_list {
				arg1, err1 := strconv.Atoi(v)
				if err1 != nil {
					return errors.New("config_buff format error 11")
				}
				myshm[key].Param1[k] = int32(arg1)
			}
		}
		//
	}

	log.Println("load config_buff completed.")

	return nil
}
